<template>
    <div class="index-container">
        <v-breadcrumb :breadcrumbData="[{name: '基础数据'}, {name: '单页文章详情'}]"></v-breadcrumb>

        <div class="g-box">
            <div class="table">
                <table class="width100 vd-table">
                    <tbody>
                    <tr>
                        <td style="width:130px">名称</td>
                        <td>
                            <el-input placeholder="请填写名称" v-model="addobj.name" class="width-400"/>
                        </td>
                    </tr>
                    <tr>
                        <td>标题图片</td>
                        <td>
                            <v-upload-image :fromData="addobj.image" @toData="uploadTitleImage"></v-upload-image>
                        </td>
                    </tr>
                    <tr>
                        <td>状态</td>
                        <td>
                            <el-select v-model="addobj.status" placeholder="请选择" class="width-400">
                                <el-option label="正常" :value="1"></el-option>
                                <el-option label="隐藏" :value="2"></el-option>
                            </el-select>
                        </td>
                    </tr>
                    <tr>
                        <td>推荐到首页</td>
                        <td>
                            <el-select v-model="addobj.is_push_to_home" placeholder="请选择" class="width-400">
                                <el-option label="推荐到小程序首页" :value="1"></el-option>
                                <el-option label="不推荐到小程序首页" :value="2"></el-option>
                            </el-select>
                        </td>
                    </tr>
                    <tr>
                        <td>推荐到我的</td>
                        <td>
                            <el-select v-model="addobj.is_push_to_my" placeholder="请选择" class="width-400">
                                <el-option label="推荐到小程序“我的”页面" :value="1"></el-option>
                                <el-option label="不推荐到小程序“我的”页面" :value="2"></el-option>
                            </el-select>
                        </td>
                    </tr>
                    <tr>
                        <td>排序</td>
                        <td>
                            <el-input placeholder="请填写排序" v-model="addobj.sort" class="width-400"/>
                        </td>
                    </tr>
                    <tr>
                        <td>详情</td>
                        <td>
                            <v-editor :fromData="addobj.content" @toData="changeContent"></v-editor>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <el-button v-preventDbClick @click="submit" type="primary" class="width-100">确定
                            </el-button>
                        </td>
                    </tr>

                    </tbody>
                </table>

            </div>
        </div>

    </div>
</template>

<script type="text/ecmascript-6">
import request from '../../common/js/request.js';
import {tool} from "../../common/js/tool.js";
import config from '../../common/js/config.js'

export default {
    data() {
        return {
            id: 0,
            addobj: {
                image: '',
                name: '',
                sort: '',
                status: 1,
                content: '',
                is_push_to_home: 2,
                is_push_to_my: 2
            }
        };
    },

    computed: {},

    mounted() {
    },

    created() {
        this.id = this.$route.query.id ? this.$route.query.id : 0;
        if (this.id) {
            this.getData();
        }
    },

    methods: {
        getData() {
            request({
                url: '/manage/single/one',
                data: {id: this.id}
            }).then((res) => {
                this.addobj.name = res.data.name;
                this.addobj.sort = res.data.sort;
                this.addobj.status = res.data.status;
                this.addobj.content = res.data.content;
                this.addobj.is_push_to_home = res.data.is_push_to_home;
                this.addobj.is_push_to_my = res.data.is_push_to_my;
                this.addobj.image = res.data.image;
            })
        },
        submit() {
            let addobj = Object.assign({}, this.addobj);
            if (this.id) {
                addobj.id = this.id;
                request({
                    url: '/manage/single/edit',
                    data: addobj
                }).then((res) => {
                    tool.message('操作成功', 'success', () => {
                        this.$router.push({path: "/basic/single-list"});
                    })
                })
            } else {
                request({
                    url: '/manage/single/add',
                    data: addobj
                }).then((res) => {
                    tool.message('操作成功', 'success', () => {
                        this.$router.push({path: "/basic/single-list"});
                    })
                })
            }
        },
        uploadTitleImage(a) {
            this.addobj.image = a;
        },
        changeContent(a) {
            this.addobj.content = a;
        }
    },

};
</script>

<style lang="less" rel="stylesheet/less" scoped>
@import url("../../common/css/variable.less");
</style>
